package com.foton.lotteryinfrastructure.infrastru.repository;

import com.foton.lotterycommon.common.Constants;
import com.foton.lotterydomain.activity.model.req.PartakeReq;
import com.foton.lotterydomain.activity.model.vo.*;
import com.foton.lotterydomain.activity.repository.IActivityRepository;
import com.foton.lotteryinfrastructure.infrastru.dao.*;
import com.foton.lotteryinfrastructure.infrastru.po.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author RenPu
 * @Date 2023/11/30 18:11
 * @Version 1.0
 * @Description: 活动模块-仓库层
 **/

@Component
public class ActivityRepository implements IActivityRepository {

    @Resource
    private IActivityDao iActivityDao;

    @Resource
    private IAwardDao iAwardDao;

    @Resource
    private IStrategyDao iStrategyDao;

    @Resource
    private IStrategyDetailDao iStrategyDetailDao;

    @Resource
    private IUserTakeActivityCountDao iUserTakeActivityCountDao;


    /**
     * 添加活动配置
     *
     * @param activity 活动配置
     */
    @Override
    public void addActivity(ActivityVO activity) {
        Activity req = new Activity();
        BeanUtils.copyProperties(activity, req);
        iActivityDao.insert(req);
    }

    /**
     * 添加奖品配置集合
     *
     * @param awardList 奖品配置集合
     */
    @Override
    public void addAward(List<AwardVO> awardList) {

        List<Award> req = new ArrayList<>();
        for (AwardVO awardVO : awardList) {
            Award award = new Award();
            BeanUtils.copyProperties(awardVO, award);
            req.add(award);
        }
        awardList.clear();
        iAwardDao.insertList(req);
    }


    /**
     * 添加策略配置
     *
     * @param strategy 策略配置
     */
    @Override
    public void addStrategy(StrategyVO strategy) {
        Strategy req = new Strategy();
        BeanUtils.copyProperties(strategy, req);
        iStrategyDao.insert(req);
    }


    /**
     * 添加策略明细配置
     *
     * @param strategyDetailList 策略明细集合
     */
    @Override
    public void addStrategyDetailList(List<StrategyDetailVO> strategyDetailList) {

        List<StrategyDetail> req = new ArrayList<>();
        for (StrategyDetailVO strategyDetailVO : strategyDetailList) {
            StrategyDetail strategyDetail = new StrategyDetail();
            BeanUtils.copyProperties(strategyDetailVO, strategyDetail);
            req.add(strategyDetail);
        }
        iStrategyDetailDao.insertList(req);
    }

    /**
     * 变更活动状态
     *
     * @param activityId  活动ID
     * @param beforeState 修改前状态
     * @param afterState  修改后状态
     * @return 更新结果
     */
    @Override
    public boolean alterStatus(Long activityId, Enum<Constants.ActivityState> beforeState, Enum<Constants.ActivityState> afterState) {
        AlterStateVO alterStateVO = new AlterStateVO(activityId, ((Constants.ActivityState) beforeState).getCode(), ((Constants.ActivityState) afterState).getCode());
        int count = iActivityDao.alterState(alterStateVO);
        return 1 == count;
    }

    @Override
    public ActivityBillVO queryActivityBill(PartakeReq req) {

        //1：根据活动ID查询活动信息数据
        Activity activity = iActivityDao.queryActivityById(req.getActivityId());
        //2:根据用户ID和活动ID，查询领取次数

        UserTakeActivityCount userTakeActivityCount = new UserTakeActivityCount();
        userTakeActivityCount.setActivityId(activity.getActivityId());
        userTakeActivityCount.setuId(req.getUserId());
        userTakeActivityCount = iUserTakeActivityCountDao.queryUserTakeActivityCount(userTakeActivityCount);

        //3: 封装数据进行返回
        ActivityBillVO activityBillVO = new ActivityBillVO();
        activityBillVO.setuId(req.getUserId());
        activityBillVO.setActivityId(req.getActivityId());
        activityBillVO.setActivityName(activity.getActivityName());
        activityBillVO.setBeginDateTime(activity.getBeginDateTime());
        activityBillVO.setEndDateTime(activity.getEndDateTime());
        activityBillVO.setTakeCount(activity.getTakeCount());
        activityBillVO.setStockSurplusCount(activity.getStockSurplusCount());
        activityBillVO.setStrategyId(activity.getStrategyId());
        activityBillVO.setState(activity.getState());
        activityBillVO.setUserTakeLeftCount(null == userTakeActivityCount ? null : userTakeActivityCount.getLeftCount());

        return activityBillVO;
    }

    @Override
    public int subtractionActivityStock(PartakeReq req) {
        return iActivityDao.subtractionActivityStock(req.getActivityId());
    }
}
